package com.hl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.Heartmark;
import com.hl.domain.SelectLike;
import com.hl.mapper.HeartmarkMapper;
import com.hl.mapper.HeartmarktopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HeartmarkService {
    @Autowired
    private HeartmarkMapper heartmarkMapper;
    @Autowired
    private HeartmarktopicMapper heartmarktopicMapper;

//    查询全部
    public List<Heartmark> selectAll(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Heartmark> heartmarkList = heartmarkMapper.selectAllHeartmark();
        for (Heartmark heartmark: heartmarkList) {
                int i = heartmarktopicMapper.selectCount(heartmark.getId());//数量
                heartmark.setTotal(i);
        }
        return heartmarkList;
    }
//    模糊查询
    public List<Heartmark> selectByName(String name,Date startTime,Date endTime){
        return heartmarkMapper.selectByName(name,startTime,endTime);
    }
//    联表查询
    public Map<String,Heartmark> selectAllAnd(Integer id, int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Heartmark> heartmarkList = heartmarkMapper.selectAllAnd(id);
        PageInfo<Heartmark> pageInfo = new PageInfo<Heartmark>(heartmarkList);
        Map map = new HashMap();
        map.put("list",heartmarkList);
        map.put("total",pageInfo.getTotal());
        return map;
    }
//    通过id删除
    public int delete(int id){
        return heartmarkMapper.deleteByPrimaryKey((long) id);
    }
//    通过id修改
    public int update(Heartmark heartmark){
        return heartmarkMapper.updateByPrimaryKey(heartmark);
    }
//    添加
    public int insert(Heartmark heartmark){
        return heartmarkMapper.insert(heartmark);
    }
 public Map<String,Object> selectAllByPage(Integer pageNo, Integer pageSize){
        Page<Object> page = PageHelper.startPage(pageNo, pageSize);
        //获取到分页后的数据
        List<Heartmark> heartmarks = heartmarkMapper.selectAllByPage();

        PageInfo<Object> info = new PageInfo<>(page.getResult());
        //获取总条数
        long total = info.getTotal();
        // 计算总页数（注意要向上取整）
        int totalPages = (int) Math.ceil((double) total / pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list",heartmarks);
        map.put("totalPageCount",totalPages);
        return map;

    }
}
